<template>
  <div class="tags-container">
    <div class="toolbar">
      <h2 class="page-title">标签管理</h2>
      <el-button type="primary" @click="showCreateDialog">
        <el-icon><Plus /></el-icon>
        新建标签
      </el-button>
    </div>
    
    <div v-loading="tagStore.loading" class="tags-list">
      <div class="tag-item" v-for="tag in tagStore.tags" :key="tag.id">
        <div class="tag-info">
          <el-tag :color="tag.color" size="large">{{ tag.name }}</el-tag>
        </div>
        <div class="tag-actions">
          <el-button type="text" size="small" @click="handleEdit(tag)">
            编辑
          </el-button>
          <el-button type="text" size="small" @click="handleDelete(tag.id)">
            删除
          </el-button>
        </div>
      </div>
      
      <el-empty v-if="!tagStore.loading && tagStore.tags.length === 0" description="暂无标签" />
    </div>
    
    <!-- 创建/编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="isEdit ? '编辑标签' : '新建标签'"
      width="500px"
    >
      <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入标签名称" />
        </el-form-item>
        <el-form-item label="颜色" prop="color">
          <el-color-picker v-model="form.color" />
        </el-form-item>
      </el-form>
      
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessageBox } from 'element-plus'
import { useTagStore } from '@/stores/tag'

const tagStore = useTagStore()

const dialogVisible = ref(false)
const isEdit = ref(false)
const currentId = ref(null)
const formRef = ref()

const form = reactive({
  name: '',
  color: '#409EFF'
})

const rules = {
  name: [
    { required: true, message: '请输入标签名称', trigger: 'blur' }
  ]
}

onMounted(() => {
  tagStore.fetchTags()
})

function showCreateDialog() {
  isEdit.value = false
  currentId.value = null
  form.name = ''
  form.color = '#409EFF'
  dialogVisible.value = true
}

function handleEdit(tag) {
  isEdit.value = true
  currentId.value = tag.id
  form.name = tag.name
  form.color = tag.color
  dialogVisible.value = true
}

async function handleDelete(id) {
  await ElMessageBox.confirm('确定要删除这个标签吗？', '提示', {
    type: 'warning'
  })
  await tagStore.removeTag(id)
}

async function handleSubmit() {
  if (!formRef.value) return
  
  await formRef.value.validate(async (valid) => {
    if (valid) {
      if (isEdit.value) {
        await tagStore.modifyTag(currentId.value, form)
      } else {
        await tagStore.addTag(form)
      }
      dialogVisible.value = false
    }
  })
}
</script>

<style scoped>
.tags-container {
  max-width: 1000px;
  margin: 0 auto;
}

.toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.page-title {
  font-size: 24px;
  font-weight: 600;
  color: #303133;
  margin: 0;
}

.tags-list {
  background: white;
  border-radius: 8px;
  padding: 24px;
  min-height: 400px;
}

.tag-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  border-bottom: 1px solid #e4e7ed;
  transition: background 0.3s;
}

.tag-item:hover {
  background: #f5f7fa;
}

.tag-item:last-child {
  border-bottom: none;
}

.tag-info {
  flex: 1;
}

.tag-actions {
  display: flex;
  gap: 8px;
}
</style>

